package com.vcs.dao;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

import com.vcs.domain.Client;
import com.vcs.web.ClientAction;

public class ClientDAOImpl implements ClientDAO {

	Log log = LogFactory.getLog(ClientDAOImpl.class);

	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	@Override
	public void deleteClient(Long clientId) {
		try {
			Client client = (Client) session.get(Client.class, clientId);
			session.delete(client);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	@Override
	public List<Client> listClient() {
		List<Client> clients = null;
		try {
			clients = session.createQuery("from Client").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return clients;
	}

	// Get client by ID.
	@Override
	public Client listClientById(Long clientId) {
		System.out.println("inside listClientById() method..");
		System.out.println("Fetching data for clientId : " + clientId);
		System.out.println("Hibernate session is : " + session);
		Client client = null;
		try {
			client = (Client) session.get(Client.class, clientId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("client: " + client);
		return client;
	}

	@Override
	public Client getProfileByUsername(String username, String password) {
		Client client = null;
		String hql = "select c.id where username='" + username
				+ "', password='" + password + "'";
		log.info("hql: " + hql);

		try {
			log.info("inside try block of getProfileByUsername() method");
			Criteria crit = session.createCriteria(Client.class)
					.add(Restrictions.like("username", username))
					.add(Restrictions.like("password", password));
			List prof = crit.list();
			client = (Client) prof.get(0);
		} catch (Exception e) {
			log.info("inside catch - Some exception occured while Client logging in.");
			transaction.rollback();
			e.printStackTrace();
		}
		log.info("Client retrieved from database is : " + client);
		return client;
	}

	@Override
	public void save(Client client) {
		try {
			log.info("save()...");
			session.save(client);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(Client client) {
		try {
			session.update(client);
		} catch (Exception e) {
			// transaction.rollback();
			e.printStackTrace();
		}
	}

	@Override
	public List<String> getAllUsernames() {
		log.info("getAllUsernames()...");
		// TODO Fix this function. duplicate usernames should not get
		// registered.
		log.info("Hibernate session is :" + session);
		List<String> usernamesList = null;
		try {
			usernamesList = session.createQuery(
					"select c.username from Client c").list();
			// List<Client> lst = session.createQuery("from Client").list();
			// log.info("---------/// " + lst);
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info("usernames List ==>: " + usernamesList);
		return usernamesList;
	}

	@Override
	public Map<String, String> getAllResumeNames() {
		log.info("getAllResumeNames()...");
				
		String hql = "select j.resume_name, j.resume_upload_date from jobseekers j";
		List lst = session.createSQLQuery(hql).list();
		
		Object[] objarr = (Object[]) lst.get(0);
		
		// set values in map
		Map<String, String> map = new HashMap<String, String>();
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

		for (Object j : lst) {
			Object[] obj = (Object[]) j;
			// String resName = null;
			// String resDate = null;

			if (obj[0] != null && obj[1] != null) {
				try {
					map.put((String) obj[0], obj[1].toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	
		log.debug("Map size : " + map.size());
		return map;
	}

	
	
	
}
